package org.itboys.mobile.controller.mobile;

import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.itboys.commons.CommonConstants;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.dto.TianJieWalletDto;
import org.itboys.mobile.dto.TianJieWalletProductDto;
import org.itboys.mobile.dto.TianJieWalletRecordDto;
import org.itboys.mobile.service.mysql.mobile.TianJieMobileWalletService;
import org.itboys.mobile.util.AccountIdHoldUtils;
import org.itboys.mobile.util.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：江雄
 * 日期：2017年5月10日
 * 描述：天街_消费卡_controller_mobile
 */
@Controller
@RequestMapping("/tianjie/mobile/finance/wallet")
public class TianJieMobileWalletController extends BaseController{
    @Autowired
    private TianJieMobileWalletService walletService;

    @RequestMapping(value="/getWallet",method= {RequestMethod.POST,RequestMethod.GET})
    @ApiOperation(value="【消费卡】获取我的消费卡（钱包）")
    public void getWallet(HttpServletRequest request,
                           HttpServletResponse response,
                           @ApiParam(value = "随机数")@RequestParam(value = "nonce", required = true) String nonce,
                           @ApiParam(value = "签名")@RequestParam(value = "sign", required = true) String sign
    ) {
        try{
            //校验签名
            Long memberId = AccountIdHoldUtils.getMemberId();
            //2017年8月28日
            TianJieWalletDto walletDto=walletService.getWallet(memberId,true);
            ResponseMessage.success(walletDto);
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/tianjie/finance/mobile/wallet/getWallet----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }

    @RequestMapping(value="/getRecord",method= {RequestMethod.POST,RequestMethod.GET})
    @ApiOperation(value="【消费卡】获取我的消费卡（钱包）交易记录")
    public void getRecord(HttpServletRequest request,
                          HttpServletResponse response,
                          @ApiParam(required = true, name = "pageSize", value = "分页参数。每页数量。默认20") @RequestParam(value = "pageSize", required = true) String pageSize,
                          @ApiParam(required = true, name = "pageNumber", value = "分页参数。第几页。默认1") @RequestParam(value = "pageNumber", required = true) String pageNumber,
                          @ApiParam(required = false, name = "sort", value = "排序参数。字段名称。默认为空") @RequestParam(value = "sort", required = false) String sort,
                          @ApiParam(required = false, name = "order", value = "排序参数。。默认为空。ASC正序/DESC倒序") @RequestParam(value = "order", required = false) String order,
                          @ApiParam(required = false, name = "type", value = "记录类型，不传就是全部（消费卡账单使用）。周薪：9，日薪：8，理财：7") @RequestParam(value = "type", required = false) Integer type,


                          @ApiParam(value = "随机数")@RequestParam(value = "nonce", required = true) String nonce,
                          @ApiParam(value = "签名")@RequestParam(value = "sign", required = true) String sign
    ) {
        try{
            //校验签名
            Long memberId = AccountIdHoldUtils.getMemberId();
            //2017年8月28日
            List<TianJieWalletRecordDto> recordDtos=walletService.getRecord(request,memberId,type);
            ResponseMessage.success(recordDtos);
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/tianjie/finance/mobile/wallet/getRecord----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }
    @RequestMapping(value="/getProducts",method= {RequestMethod.POST,RequestMethod.GET})
    @ApiOperation(value="【消费卡】获取充值产品(不分页)。按充值类型区分：1消费卡充值；2游戏充值）")
    public void getProducts(HttpServletRequest request,
                          HttpServletResponse response,
                          @ApiParam(required = true, name = "type", value = "充值产品类型。1，消费卡充值，2游戏充值") @RequestParam(value = "type", required = true) Integer type,
                          @ApiParam(value = "随机数")@RequestParam(value = "nonce", required = true) String nonce,
                          @ApiParam(value = "签名")@RequestParam(value = "sign", required = true) String sign
    ) {
        try{
            //校验签名
            Long memberId = AccountIdHoldUtils.getMemberId();
            //2017年8月28日
            List<TianJieWalletProductDto> productDtos=walletService.getProducts(type);
            ResponseMessage.success(productDtos);
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/tianjie/finance/mobile/wallet/getProducts----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }
    @RequestMapping(value="/deposit",method= {RequestMethod.POST,RequestMethod.GET})
    @ApiOperation(value="【消费卡】托管区块链，可托管金额取值字段：消费卡-钱包-unreleaseAmount")
    public void deposit(HttpServletRequest request,
                        HttpServletResponse response,
                        @ApiParam(required = true, name = "amount", value = "托管金额") @RequestParam(value = "amount", required = true) BigDecimal amount,
                        @ApiParam(value = "随机数")@RequestParam(value = "nonce", required = true) String nonce,
                        @ApiParam(value = "签名")@RequestParam(value = "sign", required = true) String sign
    ) {
        try{
            //校验签名
            Long memberId = AccountIdHoldUtils.getMemberId();
            //2017年8月28日
          //  walletService.deposit(memberId,amount);
          //  ResponseMessage.success();
            ResponseMessage.error(-1,"此业务已暂停，请联系万品天街");
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/tianjie/finance/mobile/wallet/deposit----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }

    @RequestMapping(value="/applyCash",method= {RequestMethod.POST,RequestMethod.GET})
    @ApiOperation(value="【消费卡】申请提现。备注：可提现金额取值字段：消费卡-钱包-ableCashAmount！即：周薪+日薪+理财的合计）")
    public void applyCash(HttpServletRequest request,
                          HttpServletResponse response,
                          @ApiParam(required = true, name = "bitAddress", value = "小比特地址。") @RequestParam(value = "bitAddress", required = true) String bitAddress,
                          @ApiParam(required = true, name = "amount", value = "提现金额。提现金额不能大于可提现金额,前端做好判断。") @RequestParam(value = "amount", required = true) BigDecimal amount,
                          @ApiParam(value = "随机数")@RequestParam(value = "nonce", required = true) String nonce,
                          @ApiParam(value = "签名")@RequestParam(value = "sign", required = true) String sign
    ) {
        try{
            //校验签名
            Long memberId = AccountIdHoldUtils.getMemberId();
            //2017年8月28日
       //    walletService.applyCash(memberId,bitAddress,amount);
         //   ResponseMessage.success();
            ResponseMessage.error(-1,"此业务已暂停，请联系万品天街");
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/tianjie/finance/mobile/wallet/block/applyCash----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }
}
